//
//  TransactionViewController.m
//  Balance
//
//  Created by Calvin Kuo on 12-05-01.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

/*
 Status: changed
 Last Merge Date: 06-05-2012
 Last Change Date: 
 Changes:
 1. add action, backToGraph
 */

#import "TransactionViewController.h"

@implementation TransactionViewController
@synthesize navigationItem;
@synthesize spreadView;
@synthesize bucketType = _bucketType;

#pragma mark - View lifecycle

#define trxnHeaderArray [NSArray arrayWithObjects:@"Transaction", @"Original Amount", @"Remaining Amount", @"Days Late", @"Due Date", @"Class", @"Sales Order", @"Terms",@"Primary Sales Rep", @"Operating Unit", @"Deinquency Status", @"Unpaid Reason",@"Legal Entity", @"Installment Number", nil]

#define trxnRecordArray [NSArray arrayWithObjects:@"10000000", @"10000000",@"100000", @"8", @"01-Mar-2012", @"Vision Operations", @"Invoice", @"66380",@"2/10, Net30", @"Doris Wong", @"Deinquent", @"Unknown",@"Vision Operations", @"100000", nil]

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    navigationItem.title = self.bucketType;
    trxnNumber = 0;
    originalAmount = 0;
    remainingAmount = 0;
    daysLate = 0;
    currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  
    [spreadView reloadData];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setSpreadView:nil];
  
    [self setNavigationItem:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark - Spread View Datasource

//no of columns per column section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfColumnsInSection:(NSInteger)section
{
    //return 3;
    return 13;
}

//no or rows per row section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfRowsInSection:(NSInteger)section
{
    return 30;
}

//no of column sections
- (NSInteger)numberOfColumnSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 4;
    return 1;
}

// no of row sections
- (NSInteger)numberOfRowSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 10;
    return 1;
}

#pragma mark Heights
// Comment these out to use normal values (see MDSpreadView.h)
/*
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 return 25+indexPath.row;
 }
 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowHeaderInSection:(NSInteger)rowSection
 {
 //    if (rowSection == 2) return 0; // uncomment to hide this header!
 
 return 22+rowSection*10;
 }
*/ 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnAtIndexPath:(NSIndexPath *)indexPath
 {
     switch (indexPath.row) {
         case 0:  //transaction
             return 110;
             break;
         case 1:  //original amount
             return 150;
             break;
         case 2:  //remaining amount
             return 150;
             break;
         case 3:  //days late
             return 100;
             break;
         case 4:  //due date
             return 100;
             break; 
         case 5:  //sales order
             return 130;
             break; 
         case 6:  //sales order
             return 110;
             break; 
         case 7:  //terms
             return 80;
             break; 
         default:
             return 146;
             break;
     }
    //return 220+indexPath.row*50;
 }
/* 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnHeaderInSection:(NSInteger)columnSection
 {
 //    if (columnSection == 2) return 0; // uncomment to hide this header!
 
 return 110+columnSection*50;
 }
 */
#pragma Cells
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForRowAtIndexPath:(NSIndexPath *)rowPath forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"Cell";
    
    MDSpreadViewCell *cell = [aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewCell alloc] initWithStyle:MDSpreadViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Test Row %d-%d (%d-%d)", rowPath.section+1, rowPath.row+1, columnPath.section+1, columnPath.row+1];
    // cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];

    switch (columnPath.row) {

        case 0:  //transaction
            trxnNumber = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10) + 10 * rowPath.column;
            cell.textLabel.text = [NSString stringWithFormat:@"%d", trxnNumber];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
            break; 
 
        case 1:  //original amount
            originalAmount = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10000000);
            cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:originalAmount]];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        case 2:  //remaining amount
            remainingAmount = (arc4random()%1000000);
            cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:remainingAmount]];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        case 3:  //days late
            daysLate = (arc4random()%150);
            cell.textLabel.text = [NSString stringWithFormat:@"%d", daysLate];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        default:
            cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
        break;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    return cell;
}


//There are 3 types of header cells and the following 3 methods creating these cells for each type.
//CornerHeaderCell, RowHeaderCell,ColumnHeaderCell.
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)rowSection forColumnSection:(NSInteger)columnSection
{
    static NSString *cellIdentifier = @"CornerHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleCorner reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"Cor %d-%d", columnSection+1, rowSection+1];
    
    return cell;
}
//this is actually the header for each column
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)section forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"RowHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleRow reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Row Header %d (%d-%d)", section+1, columnPath.section+1, columnPath.row+1];
    cell.textLabel.text = [trxnHeaderArray objectAtIndex:columnPath.row];
    cell.textLabel.frame = cell.frame;
    
    if ([cell.textLabel.text isEqualToString:@"Original Amount"] || [cell.textLabel.text isEqualToString:@"Remaining Amount"] || [cell.textLabel.text isEqualToString:@"Days Late"]) {
        cell.textLabel.textAlignment = UITextAlignmentRight;
    } else {
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    }

    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    return cell;
}

//this is actually the header for each row
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInColumnSection:(NSInteger)section forRowAtIndexPath:(NSIndexPath *)rowPath
{
    static NSString *cellIdentifier = @"ColumnHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleColumn reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%d (%d-%d)", section+1, rowPath.section+1, rowPath.row+1];
    
    return cell;
}



- (IBAction)backToGraph:(id)sender {
    [self.navigationController popViewControllerAnimated:NO];
    /*
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    //[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
     */
}
@end
